package com.szqhyxj.youxiangju.biz.equipmentActivation.headIdCard;

import android.Manifest;
import android.app.Activity;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.core.PhotoBridge;
import com.szqhyxj.youxiangju.biz.equipmentActivation.EquipmentActivationActivity;
import com.szqhyxj.youxiangju.sys.components.LoadImageUtils;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.components.view.popview.SingleDialog;
import com.szqhyxj.youxiangju.sys.util.FileUtils;
import com.szqhyxj.youxiangju.sys.util.ToastUtils;
import com.yanzhenjie.permission.AndPermission;

import java.io.File;
import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by leung on 2018/11/1.
 */

public class HeadIdCardActivity extends BaseActivity implements HeadIdCardContract.View, NavigationBar.NavigationBarListener {
    private static final int REQUEST_TAKE_PHOTO_HEAD = 7;
    private static final int REQUEST_GALLERY_HEAD = 8;
    private static final int PERMISS_REQUEST_CODE = 2;
    Boolean HeadFlag = false;
    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.tipTextViewcard)
    TextView tipTextViewcard;
    HeadIdCardPresenter presenter;
    @BindView(R.id.headIdCard)
    ImageView headIdCard;
    @BindView(R.id.next)
    Button next;
    String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
    public static void start(Activity activity) {
        Intent intent = new Intent();
        intent.setClass(activity, HeadIdCardActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hand_idcard);
        ButterKnife.bind(this);
        presenter = new HeadIdCardPresenter(this);
        navigationBar.setNavigationBarListener(this);
    }

    @OnClick({R.id.handIdCard, R.id.next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.handIdCard:
                AndPermission.with(this)
                        .runtime()
                        .permission(permissions)
                        .onGranted(permissions -> {
                            // Storage permission are allowed.
                            showDialog();
                        })
                        .onDenied(permissions -> {
                            // Storage permission are not allowed.
                            ToastUtils.showTost(getApplicationContext(),"沒有权限，请到设置页面授予权限.");
                        })
                        .start();
                break;
            case R.id.next:
                if (HeadFlag == true) {
                    presenter.commitHand();
                } else {
                    Toast.makeText(this, "图片地址获取失败！", Toast.LENGTH_LONG).show();
                }
                break;
        }
    }



    private void showDialog() {
        final SingleDialog singleDialog = new SingleDialog(HeadIdCardActivity.this);
        singleDialog.setISharePlatformListener(new SingleDialog.ISharePlatformListener() {
            @Override
            public void sharePlatform(String platform) {

            }

            @Override
            public void onClickAlbum(View view) {
                presenter.selectFromGalleryForHeadIcCard();
                singleDialog.dismiss();
            }

            @Override
            public void onTakePhoto(View view) {
                try {
                    presenter.TakePhotoHeadIdCard(HeadIdCardActivity.this);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                singleDialog.dismiss();
            }
        });
        singleDialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_CANCELED) {
            return;
        }
        // 拍照或者选择照片成功的时候，交给 presenter 处理
        if (requestCode == REQUEST_TAKE_PHOTO_HEAD) {
            try {
                presenter.idcardHeadFromTakePhoto(this);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        if (requestCode == REQUEST_GALLERY_HEAD) {
            File tempFile = FileUtils.uri2File(this, data.getData());
            presenter.idcardHeadFromGallery(tempFile);
        }

        navigationBar.setNavigationBarListener(this);
    }

    @Override
    public void showHeadIdCard(File file) {
        LoadImageUtils.loadImage(headIdCard, file);
    }

    @Override
    public void navToTakePhotoHeadIdCard(File file) {
        PhotoBridge.startToTakePicture(this, REQUEST_TAKE_PHOTO_HEAD, file);
    }


    @Override
    public void navToGalleryForHeadIdCard() {
        PhotoBridge.startToGallery(this, REQUEST_GALLERY_HEAD);
    }

    @Override
    public void showIDCardHeadUploadSuccess(String s) {
        Log.i("HeadIcCardActivity:", "showIDCardHeadUploadSuccess: " + s);
        HeadFlag = true;
    }

    @Override
    public void uploadSuccessHead() {
        next.setBackground(getResources().getDrawable(R.drawable.background_btn04));
        next.setEnabled(true);
    }

    @Override
    public void showIDCardHeadUploadFail(String s) {
        Log.i("HeadIcCardActivity:", "showIDCardFaceUploadFail: " + s);
        HeadFlag = false;
        Toast.makeText(this, s, Toast.LENGTH_LONG);
    }

    @Override
    public void CommitSuccess() {
        EquipmentActivationActivity.start(this);
        finish();
    }

    @Override
    public void CommitFail() {
        Toast.makeText(this, "上传失败！", Toast.LENGTH_LONG).show();
    }

    @Override
    public void showToast(String s) {
        Toast.makeText(this, s, Toast.LENGTH_LONG).show();
    }

    @Override
    public void navigationLeft() {
        finish();
    }

    @Override
    public void navigationRight() {

    }
}
